<template>
	<view class="box-wrap">
		<view class="title-wrap title">
			过去一个月的微信步数记录
		</view>
		<view class="table-wrap">
			<wyb-table :headers="headers" :contents="stepInfoList"></wyb-table>
		</view>
		<button class="auth-button" @click="getWxRunData">获取微信步数</button>
	</view>
</template>

<script>
	import WXBizDataCrypt from '@/utils/WXBizDataCrypt.js'	//微信官方提供解码js
	import wybTable from '@/components/wyb-table/wyb-table.vue'
	import formatDate from '@/utils/datetime.js'
	export default {
		data() {
			return {
				headers: [{				// 表格头部
					label: '日期',
					key: 'timestamp',
					width: 420
				},{
					label: '步数',
					key: 'step',
					width: 200
				}],
				stepInfoList: []		// 步数信息数组
			};
		},
		components: {
			wybTable
		},
		onLoad(options) {
			this.getWxRunData();
		},
		methods: {
			getWxRunData() {
				let that = this;
				uni.showLoading({
					title: '加载中'
				})
				// 登录获取code
				uni.login({
					provider: 'weixin',
					success: function(res) {
						// 通过code换取sessionKey
						uni.request({
							url: 'https://api.weixin.qq.com/sns/jscode2session',
							method: 'GET',
							data: {
								appid: 'wx40d66f771c89c281',
								secret: '5629a1b18da3ddc13fe2283e71678c87',
								js_code: res.code,
								grant_type: 'authorization_code'
							},
							success:function(req) {
								// 授权获取微信运动步数信息
								wx.getWeRunData({
									success: function(res2) {
										uni.hideLoading();
										// 对返回的加密信息进行解码
										let pc = new WXBizDataCrypt('wx40d66f771c89c281',req.data.session_key);
										let data = pc.decryptData(res2.encryptedData , res2.iv);
										that.stepInfoList = data.stepInfoList;
										// 对所获得时间信息进行格式化处理
										that.stepInfoList.forEach(item => {
											item.timestamp = that.formatDate(item.timestamp)
										})
									},
									fail:function(res2){
										uni.hideLoading();
										// 用户曾拒绝授权
										if(res2.err_code == -12006) {
											uni.showModal({
												content: '请前往设置界面，允许获取微信运动信息',
												success: function (res) {
													if (res.confirm) {
														// 打开设置界面
														wx.openSetting({
														  success (res) {
															// 返回信息包括用户是否同意授权
														    console.log(res.authSetting)
														  }
														})
													}
												}
											})
										} else {
											uni.showToast({
												title: res2.errMsg,
												icon: 'none',
												duration: 2000
											})
										}
									}
								})
							}
						})
					},
					fail:function(res){
						uni.hideLoading();
						uni.showToast({
							title: res.errMsg,
							icon: 'none',
							duration: 2000
						})
					}
				})
			},
			formatDate(date) {
				let unixTimestamp = new Date(date * 1000);
				let year = unixTimestamp.getFullYear();
				let month = unixTimestamp.getMonth() + 1;
				if(0 <= month && month <10) {
					month = '0' + month
				}
				let day = unixTimestamp.getDate();
				if(0 <= day && day <10) {
					day = '0' + day
				}
				let hour = unixTimestamp.getHours();
				if(0 <= hour && hour <10) {
					hour = '0' + hour
				}
				let minutes = unixTimestamp.getMinutes();
				if(0 <= minutes && minutes <10) {
					minutes = '0' + minutes
				}
				let second = unixTimestamp.getSeconds();
				if(0 <= second && second <10) {
					second = '0' + second
				}
				let currentdate = year + '年' + month + '月' + day + '日 ' + hour + ':' + minutes + ':' + second
				return currentdate;
			},
		}
	}
</script>

<style>
	.box-wrap {
		width: 100%;
		background-color: #FFFFFF;
		margin-left: auto;
		margin-right: auto;
	}
	.title-wrap {
		padding-top: 32rpx;
		margin-bottom: 24rpx;
		text-align: center;
		font-size: 36rpx;
		font-weight: bold;
	}
	.table-wrap {
		margin-left: 50rpx;
		margin-right: 50rpx;
	}
	.auth-button {
		width: 80%;
		margin: 58rpx auto 70rpx auto;
	}
</style>
